package com.gohome.gohomechat.model;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.gohome.gohomechat.GoHomeHelper;
import com.gohome.gohomechat.GoHomeModel;
import com.gohome.gohomechat.application.BaseApplication;
import com.gohome.gohomechat.db.GoHomeDBTools;
import com.gohome.gohomechat.model.entify.CurrentUser;
import com.gohome.gohomechat.model.modellistener.LoginModelInterface;
import com.gohome.gohomechat.presenter.callback.LoginCallBack;
import com.parse.GetCallback;
import com.parse.GetDataCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;

/**
 * Created by dllo on 16/2/29.
 */
public class LoginModel implements LoginModelInterface {
    private Handler handler;

    @Override
    public void login(final String userName, String userPsw, final LoginCallBack listener) {
        this.handler = new Handler();
        EMChatManager.getInstance().login(userName, userPsw, new EMCallBack() {
            @Override
            public void onSuccess() {
                EMGroupManager.getInstance().loadAllGroups();
                EMChatManager.getInstance().loadAllConversations();
                GoHomeModel.getInstance().setCurrentUserName(userName);

                ParseQuery<ParseObject> query = ParseQuery.getQuery("PersonalDataGoHome");
                query.whereEqualTo("username", userName);
                query.getFirstInBackground(new GetCallback<ParseObject>() {
                    @Override
                    public void done(ParseObject object, ParseException e) {
                        if (e == null) {
                            ParseFile file = object.getParseFile("head_img");
                            if (file != null) {
                                file.getDataInBackground(new GetDataCallback() {
                                    @Override
                                    public void done(byte[] data, ParseException e) {
                                        GoHomeModel.getInstance().setHeadByte(data);
                                    }
                                });
                            }
                            CurrentUser user = new CurrentUser();
                            user.setNick(object.getString("nick"));
                            user.setSingnature(object.getString("singnature"));
                            user.setBirthday(object.getString("birth"));
                            user.setSex(object.getString("sex"));
                            user.setAge(object.getString("age"));
                            user.setZodiaz(object.getString("zodiaz"));
                            user.setBoold(object.getString("boold"));
                            user.setEmail(object.getString("email"));
//                            GoHomeDBTools.init(BaseApplication.getContext());
//                            GoHomeDBTools.getInstance().deleteAllDatas_personal();

                  //          GoHomeDBTools.init(BaseApplication.getContext());
                  //          GoHomeDBTools.getInstance().deleteAllDatas_personal();

//                            GoHomeDBTools.init(BaseApplication.getContext());
//                            GoHomeDBTools.getInstance().deleteAllDatas_personal();

                            GoHomeDBTools.getInstance().insert_personal(user);
                            listener.loginSuccess();
                        } else {
                            listener.loginFailed();
                        }
                    }
                });


                //GoHomeHelper.getInstance().setUserName(userName);
            }

            @Override
            public void onError(int i, String s) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        listener.loginFailed();
                    }
                });

            }

            @Override
            public void onProgress(int i, String s) {

            }
        });
    }
}
